package cn.com.anypay.manager.controller.sdk;

import cn.com.anypay.manager.common.response.ApiResponse;
import cn.com.anypay.manager.service.MapService;
import cn.com.anysdk.map.model.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/map")
public class MapController {

    @Resource
    private MapService mapService;

    @PostMapping("/switch")
    public ApiResponse<String> switchProvider(@RequestBody Map<String, Object> config) {
        log.info("收到切换地图服务商请求，配置参数: {}", config);
        String result = mapService.switchProvider(config);
        log.info("切换地图服务商成功: {}", result);
        return ApiResponse.success("切换地图服务商成功", result);
    }

    @GetMapping("/static-map")
    public ApiResponse<Map<String, String>> staticMap(@RequestParam String address, @RequestParam int zoom) {
        String url = mapService.getStaticMapUrl(address, zoom);
        return ApiResponse.success(Map.of("url", url));
    }

    @GetMapping("/config")
    public ApiResponse<Map<String, Object>> getCurrentConfig(@RequestParam(required = false) String provider) {
        if (provider != null) {
            log.info("根据提供商获取地图配置: {}", provider);
            Map<String, Object> config = mapService.getConfigByProvider(provider);
            log.info("获取地图配置成功: {}", config);
            return ApiResponse.success(config);
        } else {
            log.info("获取当前地图配置");
            Map<String, Object> config = mapService.getCurrentConfig();
            log.info("获取地图配置成功: {}", config);
            return ApiResponse.success(config);
        }
    }

    @PostMapping("/geocode")
    public ApiResponse<LocationResult> geocode(@RequestBody String address) {
        try {
            String decodedAddress = java.net.URLDecoder.decode(address, "UTF-8");
            log.info("收到地址解析请求，地址: {}", decodedAddress);
            LocationResult result = mapService.geocode(decodedAddress);
            log.info("地址解析成功，结果: {}", result);
            return ApiResponse.success(result);
        } catch (java.io.UnsupportedEncodingException e) {
            log.error("地址解码失败: {}", e.getMessage(), e);
            return ApiResponse.fail("地址解码失败: " + e.getMessage());
        }
    }

    @PostMapping("/reverse-geocode")
    public ApiResponse<AddressResult> reverseGeocode(@RequestBody Location location) {
        log.info("收到逆地址解析请求，坐标: {}", location);
        AddressResult result = mapService.reverseGeocode(location);
        log.info("逆地址解析成功，结果: {}", result);
        return ApiResponse.success(result);
    }

    @PostMapping("/convert")
    public ApiResponse<LocationResult> convertCoordinate(
            @RequestBody Location location,
            @RequestParam CoordinateType fromType,
            @RequestParam CoordinateType toType) {
        log.info("收到坐标转换请求，原坐标: {}，源坐标系: {}，目标坐标系: {}", location, fromType, toType);
        LocationResult result = mapService.convertCoordinate(location, fromType, toType);
        log.info("坐标转换成功，结果: {}", result);
        return ApiResponse.success(result);
    }
}
